package uniLearn.db;

import java.sql.*;

public class DBConnection {
	//define
	//database connection information
	private static String DB_HOST = "127.0.0.1"; //hostname of database
	private static int DB_PORT = 3306; //port of database
	private static String DB_DBNAME = "uni_learn"; //name of database
	private static String DB_USERNAME = "adp"; //user name of login database
	private static String DB_PASSWORD = "1q2w3e4r"; //password of login
	//Driver name of MYSQL connecter (JDBC)	
	private static String DB_DRIVER = "com.mysql.jdbc.Driver";
	//Gen the JDBC URL for connection
	private static String getURL() {
		return "jdbc:mysql://"+ DB_HOST+ ":"+ DB_PORT+ "/"+ DB_DBNAME; 
	}
	
	private Statement stmt;
	private ResultSet rs;
	private Connection con;
	
	public DBConnection() throws ClassNotFoundException, SQLException {
		//reg the DB_DRIVER
		Class.forName(DB_DRIVER);
		//start the connection
		con = DriverManager.getConnection(getURL(), DB_USERNAME, DB_PASSWORD);
	}
	
	public PreparedStatement preStmt(String sqlString) throws SQLException {
		if(!preCheck())
			return null;

		stmt = con.prepareStatement(sqlString);
		return (PreparedStatement)stmt;
	}
	
	public PreparedStatement preStmt(String sqlString, int autoGeneratedKeys) throws SQLException {
		if(!preCheck())
			return null;

		stmt = con.prepareStatement(sqlString, autoGeneratedKeys);
		return (PreparedStatement)stmt;
	}
	
	/*public ResultSet select(String sqlString) throws SQLException {
		if(!preCheck())
			return null;
		
		stmt = con.createStatement();
		rs = stmt.executeQuery(sqlString);
		return rs;
	}
	
	public int insert(String sqlString) throws SQLException {
		if(!preCheck())
			return 0;
		
		stmt = con.createStatement();
		return stmt.executeUpdate(sqlString);
	}
	
	public int update(String sqlString) throws SQLException {
		return this.insert(sqlString);
	}*/

	private boolean preCheck() throws SQLException {
		if(con == null || con.isClosed())
			return false;
		
		if(stmt != null && !stmt.isClosed())
			stmt.close();
		
		//close the pre-ResultSet
		if (rs != null && !rs.isClosed())
			rs.close();
		
		return true;
	}
	
	/**Check all the object is closed
	 * @return all the object is closed or not
	 * @throws SQLException - throwed by the database object
	 */
	public boolean isClosed() throws SQLException {
		/*if (con != null && !con.isClosed())
			return false;
		if (stmt != null && !stmt.isClosed())
			return false;
		if (rs != null && !rs.isClosed())
			return false;
		
		return true;*/
		return (con != null && !con.isClosed());
	}
	
	
	/**
	 * Close all Database object in DBConnection object
	 */
	public void close() {
		try {
			if (rs != null && !rs.isClosed())
				rs.close();
			if (stmt != null && !stmt.isClosed())
				stmt.close();
			if (con != null && !con.isClosed())
				con.close();
		} catch (Exception e) {

		} finally {
			rs = null;
			stmt = null;
			con = null;
		}
	}
	
	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		this.close();
		super.finalize();
	}

	
}
